package com.goldze.mvvmhabit.ui.tab_bar.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.databinding.FragmentTabBar2Binding;
import com.goldze.mvvmhabit.ui.food.FoodActivity;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import me.goldze.mvvmhabit.utils.LogUtil;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Created by goldze on 2018/7/18.
 */

public class TabBar2Fragment extends Fragment {

    FragmentTabBar2Binding binding;
    public String bmi;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_tab_bar_2, container, false);
        binding.setFragment(this);
        initData();
        return binding.getRoot();
    }

    private void initData() {
        //三餐热量点击事件
        binding.tvModuleFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FoodActivity.class).putExtra("module_type","first"));
            }
        });
        binding.tvModuleSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FoodActivity.class).putExtra("module_type","second"));
            }
        });
        binding.tvModuleThird.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FoodActivity.class).putExtra("module_type","third"));
            }
        });

        //BMI点击监听
        binding.btModuleBmi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBmi();
            }
        });
    }

    public void getBmi() {
        String height = binding.etModuleH.getText().toString().trim();
        if (TextUtils.isEmpty(height)) {
            ToastUtils.showShort("请输入身高");
            return;
        }
        String kg = binding.etModuleKG.getText().toString().trim();
        if (TextUtils.isEmpty(kg)) {
            ToastUtils.showShort("请输入体重");
            return;
        }

        try {
            float h = Float.parseFloat(height);
            DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
            double hh = Double.parseDouble(decimalFormat.format(h));

            float k = Float.parseFloat(kg);
            double kk = Double.parseDouble(decimalFormat.format(k));

            LogUtil.e("BMI-->"+ div(kk,hh*hh,2));
            double d = div(kk,hh*hh,2);
            binding.tvModuleBim.setText(String.valueOf(d));

            //医生意见
            if (d < 18.5){
                binding.tvModuleAdvice.setText("偏轻");
            }else if (18.5 < d && d < 24.9){
                binding.tvModuleAdvice.setText("正常");
            }else if (25 < d && d < 29.9){
                binding.tvModuleAdvice.setText("超重");
            }else if (30 < d && d < 34.9){
                binding.tvModuleAdvice.setText("肥胖");
            }else if (35 < d ){
                binding.tvModuleAdvice.setText("过度肥胖");
            }

        } catch (Exception e) {
            LogUtil.e("体重计算有误-->"+ e.getMessage());
            ToastUtils.showShort("" + e.getMessage());
        }
    }

    //BMI结果处理
    public static double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


}
